const NewsCate = require('../models/newscate.model');

// 新
exports.create = function(req, res, next) {
  const newscate = new NewsCate(req.body);
  newscate.save().then(data => {
    res.json(data);
  });
};


//删
exports.remove = function(req, res, next) {
  var id = req.params.id;
  var ids=[];
  NewsCate.findOne({_id: id}, function(err, doc){
    //console.log(doc)
    if(doc){
      ids[0]=doc._id
      doc.getChildren(function(err, docs){
          docs.forEach(function(value,index){
            ids.push(value._id)
          })
          //console.log(ids)
          NewsCate.remove({ _id: { $in: ids } }).then(data => {
            res.json({ message: '数据删除成功', status: 200 });
          });
      });
    }
  });
};

// 改
exports.update = function(req, res, next) {
  var id = req.params.id;
  NewsCate.findByIdAndUpdate(id, { $set: req.body }, { new: true }).then(data => {
    // 千万小心，这里的data是没有更新之前的数据
    res.json(data);
  });
};

exports.getData = function(req, res, next) {
  var id = req.params.id;
  NewsCate.findById(id).then(data => {
    res.json(data);
  });
};

function reverseTree(data, pid) {
  var result = [],
    temp;

  var data = JSON.parse(JSON.stringify(data)); // 将express的model转成js的object类型
  for (var i in data) {
    if (data[i].parentId === pid) {
      result.push(data[i]);

      temp = reverseTree(data, data[i]._id);
      if (temp.length > 0) {
        data[i].children = temp;
      }
    }
  }
  return result;
}

exports.list = function(req, res, next) {
  // var type = req.params.type;
  // NewsCate.find({ type: type }).then(data => {
  //   var rpTree = reverseTree(data, null);
  //   res.json(rpTree);
  // });
  NewsCate.find({type:1}).then(data=>{
    var rpTree = reverseTree(data, null);
    res.json(rpTree)
  })
};
